﻿using System.Diagnostics;

namespace DimensionsHelper.LanguageService.Syntax;

public class WhileStatement : BodiedStatement
{
    private readonly SyntaxToken _whileKeywordToken;
    private Expression _conditionExpression;

    internal WhileStatement(SyntaxToken whileKeywordToken, SyntaxNode? parent) :
        base(SyntaxKinds.WhileStatement, parent)
    {
        SetChildField(ref _whileKeywordToken, whileKeywordToken);
        _conditionExpression = EmptyExpression.Default;
    }


    public SyntaxToken WhileKeywordToken => _whileKeywordToken;

    public Expression ConditionExpression => _conditionExpression;


    public WhileStatement Update(Expression conditionExpression)
    {
        SetChildField(ref _conditionExpression, Expression.IsEmptyExpression, conditionExpression);
        return this;
    }


    public override void AcceptVisitor<T>(SyntaxNodeVisitor<T> visitor)
    {
        Accept(visitor, WhileKeywordToken, ConditionExpression);
        base.AcceptVisitor(visitor);
    }
}


public static partial class NodeFactory
{
    public static WhileStatement CreateWhileStatement(SyntaxNode? parent, SyntaxToken whileKeywordToken,
        Expression? conditionExpression)
    {
        Debug.Assert(whileKeywordToken.IsNode(SyntaxKinds.WhileKeywordToken));
        WhileStatement stat = new(whileKeywordToken, parent);
        if (!Expression.IsEmptyExpression(conditionExpression))
        {
            stat.Update(conditionExpression);
        }

        return stat;
    }
}